package riofrio;

import java.util.Iterator;
import java.util.ListIterator;
import java.util.Stack;
import riofrio.TreeNode;

/**
 *
 * @author Victor Hugo Herrera Maldonado
 */
class PreOrderDepthFirstWalkIterator<T> implements Iterator<TreeNode<T>> {
    private final Stack<TreeNode<T>> stack=new Stack<>();
    private TreeNode<T> top;

    PreOrderDepthFirstWalkIterator(TreeNode<T> root) {
        stack.push(null);
        top=root;
    }

    @Override
    public boolean hasNext() {
        return top != null;
    }

    @Override
    public TreeNode<T> next() {
        ListIterator<TreeNode<T>> reverseIterator = top.reverseIterator();
        while(reverseIterator.hasNext()) {
            stack.push(reverseIterator.next());
        }
        TreeNode<T> tmp=top;
        top=stack.pop();
        return tmp;
    }
    
}
